<?php
require 'facebook.php';

// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
  'appId'  => APPID,
  'secret' => SECRET,
  'cookie' => true,
));

$session = $facebook->getSession();

$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}

if(@$_SESSION['username_email']==''){
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();

  //CHECK IF USER IN DATABASE
  include_once('db_config/db_user.php');
  $user_fb=new user();
  $rs_fb = $user_fb->uniq_user_list('facebook_id',$me[id]);
  $data_fb = mysql_fetch_array($rs_fb);
  if(!$data_fb){
	//Check if email address is already registered
	$rs_fb = $user_fb->uniq_user_list('user_primary_email',$me[email]);
	$data_fb_email = mysql_fetch_array($rs_fb);
	if(!$data_fb_email){
		$sql="INSERT into ninerr_user set 
			user_fullname='".$me[name]."',
			user_name='".$me[first_name]."',
			user_nickname='".$me[first_name]."',
			user_password='".rand(999,9999999)."',
			user_primary_email='".$me[email]."',
			facebook_id='".$me[id]."'
			";
		mysql_query($sql);
	}
	//User email already in database
	else{
		$sql="UPDATE ninerr_user set 
			facebook_id='".$me[id]."' 
			where user_primary_email='".$me[email]."'
			";
		mysql_query($sql);
	}
  }
  //Now make sure our facebook user is logged in to system
	$_SESSION['username_email']= $me[email];
	$rs_user=$user->uniq_user_list('user_primary_email',$me[email]);
	$data_user=mysql_fetch_array($rs_user);
	$_SESSION['user_id']=$data_user['user_id'];
	$_SESSION['user_name']=$data_user['user_name'];
	header("Location: index.php");
}
}

if(@$_SESSION['user_name'])
{
?>
<a class="trigger" href="#"><?php echo ucfirst($_SESSION['user_name']); ?></a>
<?php
}
?>

    <div id="fb-root"></div>
    <script>
      window.fbAsyncInit = function() {
        FB.init({
          appId   : '<?php echo $facebook->getAppId(); ?>',
          session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
          status  : true, // check login status
          cookie  : true, // enable cookies to allow the server to access the session
          xfbml   : true // parse XFBML
        });

        // whenever the user logs in, we refresh the page
        FB.Event.subscribe('auth.login', function() {
          window.location.reload();
        });
      };

      (function() {
        var e = document.createElement('script');
        e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
        e.async = true;
        document.getElementById('fb-root').appendChild(e);
      }());
    </script>

	
<div class="header">
<a href="<?php echo 'http://'.$_SERVER['HTTP_HOST'];?>/"><span class="logo"></span></a>
<div class="sub-header">
	<div class="awesome" >
	    <ul class="menu clearfix">
	      <li> <a href="index.php"><span>Home</span></a></li>
	      <?php
	if(@$_SESSION['username_email']=='')
	{
	?>
	      <li> <a href="how_work.php" ><span>How it Works </span></a></li>
	      <?php
	}
	?>
	      <?php
	if(@$_SESSION['username_email']!='')
	{
	?>
	      <li> <a href="user_gigs.php"><span>My Boxes</span></a></li>
	      <li> <a href="manage_work.php"><span>Built Status</span></a></li>
	      <li> <a href="manage_order.php"><span>Manage Orders</span></a></li>
	      <li> <a href="sales_balance.php"><span>Earnings</span></a></li>
	      <li> <a href="profile_edit.php"><span>Profile</span></a></li>
	      <li> <a href="messages.php" ><span>Messages</span></a></li>
	      <li> <a href="bookmarks.php" ><span>Favourites</span></a></li>
	      <li><a href="logout.php"><span>Log Out</span></a></li>
	      <?php
	}
	else
	{
	?>
	      <li> <a href="#" id="login_link1"><span>Join </span></a></li>
	      <li> <a href="#" id="login_link"><span> 
	        <?php
		echo 'Sign In';
	?>
	        </span></a></li>
	      <li> <a href="help.php" id="login_link1"><span>Help </span></a></li>
	      <?php
	}
	?>
	      <?php
	if(@$_SESSION['username_email']=='')
	{
	?>
	      <div class="fbc"> 
	        <?php if ($me): ?>
	        <a href="<?php echo $logoutUrl; ?>"> <img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif" /> 
	        </a> 
	        <?php else: ?>
	        <fb:login-button perms="email"></fb:login-button> 
	        <?php endif ?>
	      </div>
	      <?php
	}
	?>
	    </ul>
	</div>
</div>

<?php include('login_box.php'); ?>
<?php include('registration_box.php'); ?></div>
<div style="clear:both;"></div>